<template>
  <el-dialog :visible="value" :before-close="cancel">
    <template slot="title">
      上分设置
    </template>

    <el-form :model="form" :rules="refRules" ref="refForm" label-width="180px">
      <el-form-item label="近3个月每月平均佣金：" prop="commission">
        <el-col :span="16">
          <el-input :value="amount" disabled  @keyup.enter.native="doSave({action:'saveForm',form:'refForm'})">
            <template slot="append">元</template>
          </el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="状态：" prop="status">
        <el-col :span="16">
          <el-select v-model="form.status" style="width: 100%;">
            <el-option
              v-for="item in consts.AGENCY_CREDIT_STATUS"
              :label="item.title"
              :value="item.value"
              :key="item.value"
            ></el-option>
          </el-select>
        </el-col>
      </el-form-item>
      <el-form-item label="上分金额：" prop="amount">
        <el-col :span="16">
          <el-input v-model.number="form.amount" @keyup.enter.native="doSave({action:'saveForm',form:'refForm'})">
            <template slot="append">元</template>
          </el-input>
        </el-col>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="doSave({action:'saveForm',form:'refForm'})">
        确认
      </el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  getAgencyCredit, getAgencyCreditInfo, updateAgencyCredit, getAgencyCreditAmount,
} from '@/services/api/agency';
import { form, grid, factory } from '@/build';

export default {
  name: 'edit-upper-score-dialog',
  props: ['value', 'rowData'],
  mixins: [form, grid, factory],
  data() {
    return {
      form: {
        userId: this.rowData.id,
        amount: undefined,
      },
      amount: 0,
      creditAmount: 0,
      amountCount: 0,
    };
  },
  computed: {
    refRules() {
      return {
        amount: [
          { required: true, message: '上分金额必须填写' },
          { type: 'number', message: '上分金额必须填写数字' },
          { validator: factory.getValidator('nonNegativeInteger') },
          {
            type: 'number',
            min: 0,
            max: 30000000,
            message: '上分金额必须在0到30000000之间',
            trigger: ['change', 'blur'],
          },
        ],
      };
    },
  },
  methods: {
    cancel() {
      this.$emit('input', false);
    },
  },
  mounted() {
    this.bindGet(getAgencyCredit, 'form', {
      sendingData: { proxyId: this.rowData.id },
      callback: ([result, data]) => {
        if (data) {
          this.form = data;
          this.amountCount = data.amount || 0;
        }
      },
    });
    this.bindGet(getAgencyCreditInfo, 'amount', {
      sendingData: { proxyId: this.rowData.id },
    });
    // this.bindGet(getAgencyCreditAmount, 'creditAmount', {
    //   sendingData: {'proxyId': this.rowData.id}
    // });
    this.getAll();
    this.bindSave(updateAgencyCredit, 'saveForm', 'form', {
      beforeSend: (data) => {
        const { proxyId, status, amount } = data;
        const obj = {
          proxyId,
          status,
          amount,
        };
          // obj.amount = this.rowData.parentId === 1 ? amount : this.amountCount;
        return obj;
      },
      afterSuccess: () => {
        this.cancel();
        this.$parent.getAll();
      },
    });
  },
};
</script>

<style scoped>

</style>
